package com.feng.springboothelloworld.dao;

import com.feng.springboothelloworld.domain.Department;
import com.feng.springboothelloworld.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {
    static Map<Integer, Employee>employeeMap=null;
    @Autowired
    private DepartmentDao departmentDao;
    static {
        employeeMap=new HashMap<>();
        employeeMap.put(1001,new Employee(1001,"AA","1135508760@qq" +
                ".com",1,new Department(101,"财政部"),new Date()));
        employeeMap.put(1002,new Employee(1002,"AA","1135508760@qq" +
                ".com",0,new Department(102,"市场部"),new Date()));
        employeeMap.put(1003,new Employee(1003,"AA","1135508760@qq" +
                ".com",1,new Department(103,"教研部"),new Date()));
        employeeMap.put(1004,new Employee(1004,"AA","1135508760@qq" +
                ".com",0,new Department(104,"营业部"),new Date()));
        employeeMap.put(1005,new Employee(1005,"AA","1135508760@qq" +
                ".com",1,new Department(105,"后勤部"),new Date()));
    }
    private static Integer initId=1006;
    public void save(Employee employee){
        if(employee.getId()==null)employee.setId(initId++);
        employee.setDepartment(departmentDao.getbyid(employee.getDepartment().getId()));
        employeeMap.put(employee.getId(),employee);
    }
    public Collection getAll(){
        return employeeMap.values();
    }
    public Employee getByid(Integer id){
        return employeeMap.get(id);
    }
    public void delete(Integer id){
        employeeMap.remove(id);
    }
}
